package com.ruoyi.web.controller.property;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OrderInfoBO;
import com.ruoyi.system.domain.PropertyFeedback;
import com.ruoyi.system.domain.Story;
import com.ruoyi.system.service.IStoryService;
import com.ruoyi.system.service.PropertyService;
import com.ruoyi.system.service.TaskService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/property")
public class PropertyController extends BaseController {

    @Autowired
    private IStoryService storyService;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private TaskService taskService;

    private String prefix = "property";

    /**
     * 信息展示页面跳转
     * @return
     */
    @RequiresPermissions("property:user:view")
    @GetMapping("/admin/{id}")
    public String admin(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("sumCount",  storyService.selectSumChatCount());
        if("3".equals(id)){
            return "property" + "/adminThree";
        }else if("5".equals(id)){
            return "property" + "/adminFive";
        }else if("7".equals(id)){
            return "property" + "/adminSeven";
        }else {
            return "property" + "/admin";
        }
    }
    /**
     * 反馈显示弹窗
     * @return
     */
    @RequiresPermissions("property:user:view")
    @Log(title = "反馈", businessType = BusinessType.OTHER)
    @GetMapping("/admin/feedback/{id}")
    public String manage(@PathVariable("id") int id, ModelMap mmap){
        System.out.println(id);
//        Story story = storyService.selectStateById(id);
//        mmap.put("id", story.getId());
//        mmap.put("handleState", story.getHandleState());
//        mmap.put("problem", story.getProblem());
//        mmap.put("handleTime", story.getHandleTime());
//        mmap.put("forcedSubmitOrder", story.getIsForcedSubmit());

//        int handleState = storyService.selectStateById(id);

        OrderInfoBO orderInfoBO = propertyService.getOrderInfoById(id);
        PropertyFeedback propertyFeedback = propertyService.getFeedbackById(id);
        mmap.put("order",orderInfoBO);
        mmap.put("describe",orderInfoBO.getDescribe());
        mmap.put("feedback",propertyFeedback);
        return prefix + "/feedback";
    }


//    @RequiresPermissions("property:user:manage")
    @PostMapping("/admin/feedback/insert")
    @ResponseBody
    public AjaxResult insert(
            @Param("orderId") int orderId,
            @Param("keyId") String keyId,
            @Param("describe") String describe,
            @Param("firstComment") String firstComment,
            @Param("secondComment") String secondComment,
            @Param("thirdComment") String thirdComment)
    {
        System.out.println(orderId);
        System.out.println(firstComment);
        PropertyFeedback propertyFeedback = propertyService.getFeedbackById(orderId);
        if(propertyFeedback == null){
            propertyFeedback = new PropertyFeedback();
            propertyFeedback.setOrderId(orderId);
            propertyFeedback.setFirstComment(firstComment);
            propertyFeedback.setStatus(0);
            propertyService.insertFeedback(propertyFeedback);

            //第一次反馈发送及时信息到app
            //获取imaccount
            String imAccount = "";
            if (String.valueOf(keyId).length() == 10){
                //根据KEY_ID查询IM_ACCOUNT
                imAccount = storyService.getStuImByKeyId(keyId);
            }else {
                imAccount = storyService.getEmpImByKeyId(keyId);
            }
            String msg = "您提交的物业报修问题："+describe+",反馈是："+firstComment;
            System.out.println("IM_ACCOUNT = " + imAccount);
            taskService.send(imAccount,"wybx",msg);
        }else{
            int status = propertyFeedback.getStatus();
            if(status == 1){
                propertyFeedback.setSecondComment(secondComment);
                propertyFeedback.setStatus(2);
                propertyService.updateFeedback(propertyFeedback);
//                String msg = "物业报修问题："+describe+",已经5天未完成，第一次反馈是："+firstComment;
//                taskService.send("200003499","wybx",msg);
//                taskService.send("200010308","wybx",msg);
            }
            if(status == 3){
                propertyFeedback.setThirdComment(thirdComment);
                //第二次回复设置3
                propertyFeedback.setStatus(4);
                propertyService.updateFeedback(propertyFeedback);
//                String msg = "物业报修问题："+describe+",已经7天未完成，第一次反馈是："+firstComment+"；第二次反馈是："+secondComment;
//                taskService.send("200003498","wybx",msg);
            }
        }

        return success();
    }

    @RequiresPermissions("property:user:view")
    @PostMapping("/admin/listFeedback")
    @ResponseBody
    public TableDataInfo listFeedback() throws Exception {
        startPage();
        //
        List<OrderInfoBO> orderInfoList = propertyService.getFeedback();
        if (orderInfoList.size() == 0 || orderInfoList.isEmpty()){
            return getDataTable(new ArrayList<>());
        }else {
            return getDataTable(orderInfoList);
        }
    }
    /**
     * 未解决订单
     * @return
     */
    @RequiresPermissions("property:user:view")
    @PostMapping("/admin/list/{id}")
    @ResponseBody
    public TableDataInfo listThree(@PathVariable("id") int id) throws Exception {
        startPage();
        //添加当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String createTime = dateFormat.format(now);//格式化然后放入字符串中
        Calendar c = Calendar.getInstance();

        c.add(Calendar.DATE, - id);

        Date time = c.getTime();

        String preDay = dateFormat.format(time);

        int status = 0;
        if(id == 5){
            status = 1;
        }
        if(id == 7){
            //查询第二次已提醒
            status = 3;
        }
        List<OrderInfoBO> orderInfoList = propertyService.getOrderInfoByCreateTime(preDay,id,status);
        if (orderInfoList.size() == 0 || orderInfoList.isEmpty()){
            return getDataTable(new ArrayList<>());
        }else {
            return getDataTable(orderInfoList);
        }
    }
}
